package cn.th.service.impl;

import cn.th.entity.School;
import cn.th.entity.Task;
import cn.th.mapper.TaskMapper;
import cn.th.service.ITaskService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 任务表 服务实现类
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    @Autowired
    private ITaskService taskService;

    @Autowired
    private TaskMapper taskMapper;
    /**
     * 查询任务表
     * @param page
     * @param pageSize
     * @param school 学校名
     * @param publishName 发布学院名字
     * @param status 状态
     * @param points 积分
     * @param name 任务名字
     * @return
     */
    @Override
    public Map<String, Object> queryTask(int page, int pageSize, String school, String publishName, String status, String points, String name, String importance) {
        Page<Task> page1 = new Page<>(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(StringUtils.hasLength(school),Task::getSchool,school);
        wrapper.eq(StringUtils.hasLength(publishName),Task::getPublishName,publishName);
        wrapper.eq(StringUtils.hasLength(status),Task::getStatus,status);
        wrapper.eq(StringUtils.hasLength(points),Task::getPoints,points);
        wrapper.like(StringUtils.hasLength(name),Task::getName,name);
        if (StringUtils.hasLength(importance)){
            if (importance =="")
                wrapper.eq(Task::getName,name);
        }
        //添加排序条件,根据id升序排
        wrapper.orderByAsc(Task::getId);

        //执行查询
        taskService.page(page1,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total",page1.getTotal());
        data.put("rows",page1.getRecords());

        return data;
    }

    @Override
    public void removeBatchByIds(String ids) {
        taskMapper.removeBatchByIds(ids);
    }
}
